<link rel="import" href="../../bower_components/polymer/polymer-element.html">

<link rel="import" href="../../elements/som-rev-range/som-rev-range.html">
<link rel="import" href="../../elements/som-layout-test-builder-configs-behavior.html">
<link rel="import" href="../../elements/som-linkify-behavior.html">
<link rel="import" href="../../elements/som-test-results/som-master-results.html">

<dom-module id="som-extension-build-failure">
  <template>
    <style>
     a.builder {
        border-radius: 2px;
        border: 1px solid #dcdcdc;
        font-size: smaller;
        margin: .1em;
        padding: .3em 0.5em;
        display: inline-block;
        background: #e75d54;
        font-weight: bold;
        text-decoration: none;
        color: #fff;
     }
     a.builder:hover {
       text-decoration: underline;
     }
     .builder.infra-failure {
       background: #e0b0ff;
       color: #fff;
     }
     .suspect-cl-results {
       color: #ff8c00;
     }
     .revert-cl {
       color: #ff4c00;
     }
     .header {
       color: #000;
     }
     .section {
       padding-bottom: 1em;
     }
     div.section:last-of-type {
       padding-bottom: 0;
     }
     .infra-failure a {
       color: #fff;
     }
     .build-failures {
       width: 100%;
       max-height: 200px;
       overflow-y: auto;
       padding-right: 20px;
     }
     #builders,
     #finditResults,
     #reasons,
     #regressionRanges {
       margin: .75em auto;
     }
    </style>
    <div id="builders">
      <template is="dom-if" if="[[_haveBuilders(extension)]]">
        <div class="header">
          Builders this step failed on:
        </div>
        <div class="layout horizontal wrap">
          <template is="dom-repeat" items="[[extension.builders]]" as="builder">
            <a class$="[[_classForBuilder(builder)]]" target="_blank" href$="[[_linkify(linkStyle, builder.url)]]" title$="Failing for the last [[_failureCount(builder)]] build(s): [[builder.first_failure]] - [[builder.latest_failure]].">
                [[builder.name]]
                [[_failureCountText(builder)]]
            </a>
         </template>
        </div>
      </template>
    </div>
    <div class="section">
      <template is="dom-if" if="[[!_haveTests(extension.reason.tests)]]">
        No test result data available.
      </template>
      <template is="dom-if" if="[[_haveTests(extension.reason.tests)]]">
        <div class="header">
          [[_testText(extension.reason.tests, extension.reason.num_failing_tests)]]:
        </div>
        <div id="reasons" class="build-failures">
          <ul>
            <template is="dom-repeat" items="[[extension.reason.tests]]" as="test" sort="_sortTests">
              <li id="test_results">
                <a target="_blank" href$="[[_linkForTest(extension.reason, test.test_name)]]">
                  [[test.test_name]]
                </a>
                (
                <a target="_blank" href$="[[_linkToCSForTest(test.test_name)]]">Code Search</a>
                <template is="dom-if" if="[[_haveTestExpectations(test)]]">
                  , <a href$="[[_linkToEditForTest(extension.builders, test.test_name)]]">edit test expectations</a>
                </template>
                )
                <template is="dom-if" if="[[_haveArtifacts(test)]]">
                  <ul class="artifacts">
                    <template is="dom-repeat" items="[[test.artifacts]]" as="artifact">
                      <li><a target="_blank" href="[[artifact.location]]">[[artifact.name]]</a>
                    </template>
                  </ul>
                </template>
                <template is="dom-if" if="[[_hasResultHistory(extension.reason)]]">
                  <template is="dom-repeat" items="[[extension.reason.alert_test_results]]" as="testResults">
                    <template is="dom-repeat" items="[[testResults.master_results]]" as="masterResults">
                      <som-master-results master-results="[[masterResults]]">
                      </som-master-results>
                    </template>
                  </template>
                </template>
                <template is="dom-if" if="[[_hasSuspect(test)]]">
                  <template is="dom-repeat" items="[[test.suspected_cls]]" as="testCL">
                    <img src="/images/findit.png" alt="findit" title="findit" height="20" width="20">
                    <span class="suspect-cl-results">
                      Findit (<a target="_blank" href="https://goto.google.com/findit-manual">?</a>)
                      [[_finditApproach(testCL)]]
                      <a href$="https://chromium.googlesource.com/chromium/src/+/[[testCL.revision]]" target="_blank">
                          [[_textForCL(testCL.revision)]]
                      </a>
                    </span>
                  </template>
                </template>
                <template is="dom-if" if="[[_isFlaky(test)]]">
                  <img src="/images/findit.png" alt="findit" title="findit" height="20" width="20">
                  <span class="suspect-cl-results">
                    Findit
                    (<a target="_blank" href="https://goto.google.com/findit-manual">?</a>)
                    results: Flaky
                  </span>
                </template>
              </li>
            </template>
          </ul>
        </div>
      </template>
    </div>
    <div id="regressionRanges" class="section">
      <template is="dom-if" if="[[!_haveRegressionRanges(extension.regression_ranges)]]">
        No regression range information available.
      </template>
      <template is="dom-if" if="[[_haveRegressionRanges(extension.regression_ranges)]]">
        <template is="dom-repeat" items="[[extension.regression_ranges]]" as="regressionRange" filter="_showRegressionRange">
          <div>
            <som-rev-range range="[[regressionRange]]"></som-rev-range>
          </div>
        </template>
      </template>
    </div>
    <div id="finditResults" class="section">
      <template is="dom-if" if="[[_finditNotSupport(extension)]]">
        <img src="/images/findit.png" alt="findit" title="findit" height="20" width="20">
        Not supported by Findit.
        <br>
      </template>
      <template is="dom-if" if="[[_finditIsRunning(extension)]]">
        <img src="/images/findit.png" alt="findit" title="findit" height="20" width="20">
        Findit is Analyzing...
        <template is="dom-if" if="[[_finditHasUrl(extension)]]">
          <a target="_blank" href$="[[extension.findit_url]]">
            Details
          </a>
        </template>
        <br>
      </template>
      <template is="dom-if" if="[[_finditFoundNoResult(extension)]]">
        <img src="/images/findit.png" alt="findit" title="findit" height="20" width="20">
        Findit found no culprit.
        <br>
      </template>
      <template is="dom-if" if="[[_haveSuspectCLs(extension)]]">
        <template is="dom-repeat" items="[[_suspectedCls]]" as="cl">
          <template is="dom-if" if="[[_haveRevertCL(cl)]]">
            <img src="/images/findit.png" alt="findit" title="findit" height="20" width="20">
            <template is="dom-if" if="[[_revertIsCommitted(cl)]]">
              <strong class="revert-cl">
                Findit
                (<a target="_blank" href="https://goto.google.com/findit-manual">?</a>)
                has COMMITTED a
                <a target="_blank" href$="[[cl.revert_cl_url]]">revert CL</a>
                 for culprit
              </strong>
              <a target="_blank" href$="[[_linkForCL(cl.revision)]]" class="suspect-cl-link">
                [[_textForCL(cl.commit_position, cl.revision)]]</a>
              <strong class="revert-cl">.</strong>
            </template>
            <template is="dom-if" if="[[!_revertIsCommitted(cl)]]">
              <strong class="revert-cl">
                Findit
                (<a target="_blank" href="https://goto.google.com/findit-manual">?</a>)
                has CREATED a
                <a target="_blank" href$="[[cl.revert_cl_url]]">revert CL</a>
                 for culprit
              </strong>
              <a target="_blank" href$="[[_linkForCL(cl.revision)]]" class="suspect-cl-link">
                [[_textForCL(cl.commit_position, cl.revision)]]</a>
              <strong class="revert-cl">. PLEASE REVIEW. </strong>
            </template>
          </template>
          <template is="dom-if" if="[[!_haveRevertCL(cl)]]">
            <img src="/images/findit.png" alt="findit" title="findit" height="20" width="20">
            <strong class="suspect-cl-results">
              Findit
              (<a target="_blank" href="https://goto.google.com/findit-manual">?</a>)
              [[_finditApproach(cl)]]
              <a target="_blank" href$="[[_linkForCL(cl.revision)]]" class="suspect-cl-link">
                [[_textForCL(cl.commit_position, cl.revision)]]
              </a>
              with [[_finditConfidence(cl)]]% confidence.
            </strong>
          </template>
          <a target="_blank" href$="[[extension.findit_url]]">
            More details...
          </a>
          <br>
        </template>
      </template>
      <template is="dom-if" if="[[_haveCulprits(extension)]]">
        <template is="dom-repeat" items="[[_culprits]]" as="culprit">
          <img src="/images/findit.png" alt="findit" title="findit" height="20" width="20">
          <strong class="suspect-cl-results">
            Findit
            (<a target="_blank" href="https://goto.google.com/findit-manual">?</a>)
             found culprit
            <a target="_blank" href$="[[_linkForCulprit(culprit)]]" class="suspect-cl-link">
              [[_textForCL(culprit.commit.commit_position, culprit.commit.id)]]
            </a>
          </strong>
          <br>
        </template>
      </template>
    </div>
  </template>
  <script src="som-extension-build-failure.js"></script>
</dom-module>
